package com.carleasoft.mps.cms.business.article.controller;

import com.carleasoft.mps.cms.business.article.bean.entity.AuthorityCategoryEntity;
import com.carleasoft.mps.cms.business.article.bean.po.CategoryInsertPo;
import com.carleasoft.mps.cms.business.article.bean.po.CategoryPo;
import com.carleasoft.mps.cms.business.article.service.CategoryService;
import com.carleasoft.mps.cms.business.article.bean.vo.CategoryNameTreeVo;
import com.carleasoft.mps.core.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>Description: CategoryController</p>
 * @since 2021/7/5
 * @author
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    private final String parent_code = "0";
    /**
     * 树接口查询
     */
    @PostMapping("/tree")
    public R categoryTree(@RequestBody CategoryPo categoryPo) {
        List<CategoryNameTreeVo> allCategoryName = categoryService.categoryTree(categoryPo);
        return  R.ok(allCategoryName == null?new String[0] : allCategoryName);
    }

    /**
     * 后台栏目数展示
     */
    @PostMapping("/categoryPage")
    public R categoryTreePage(@RequestBody CategoryPo categoryPo) {
        List<CategoryNameTreeVo> allCategoryName = categoryService.findAllCategoryTree(categoryPo);
        return  R.ok(allCategoryName == null?new String[0] : allCategoryName);
    }
    /**
     *  栏目编辑
     * @param categoryInsertPo
     * @return
     */
    @PostMapping("/updateCategory")
   public R updateCategory(@RequestBody CategoryInsertPo categoryInsertPo, HttpServletRequest request) {
       categoryService.updateCategory(categoryInsertPo);
       return R.ok();
   };

    /**
     * 添加栏目
     * @param categoryInsertPo
     * @param request
     * @return
     */
    @PostMapping("/insertCategory")
    public R insertCategory(@RequestBody CategoryInsertPo categoryInsertPo, HttpServletRequest request) {
        categoryService.insertCategory(categoryInsertPo);
        return R.ok();
    };



    /**
     * 修改权重值
     * @param authorityCategoryEntity
     * @return
     */
    @PostMapping("/update/weight")
    public R updateWeight(@RequestBody AuthorityCategoryEntity authorityCategoryEntity){
        categoryService.updateWeight(authorityCategoryEntity);
        return R.ok();
    }
}
